package com.xiaoxx.rabbitmq.config;


import com.xiaoxx.rabbitmq.bean.MyMessage;
import com.xiaoxx.rabbitmq.constant.MessageType;
import com.xiaoxx.rabbitmq.db.service.MessageStoreService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 消息拦截器
 * @author qh123
 */
@Aspect
@Component
public class RabbitListenerAspect {
    @Resource
    private MessageStoreService messageStoreService;

    // 拦截所有带有 @RabbitListener 注解的方法
    @Before("@annotation(rabbitListener)")
    public void beforeRabbitListener(JoinPoint joinPoint, RabbitListener rabbitListener) {
        Object[] args = joinPoint.getArgs();
        MyMessage<Object> msg = (MyMessage<Object>)args[0];
        if (MessageType.RELIANT.equals(msg.getMessageType())){
            messageStoreService.copCountIncrease(msg.getMessageId());
        }
    }
}
